﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// SFUI_ScrollRect组件[Item]
/// </summary>
public partial class SFUI_ScrollRect
{
    /// <summary>
    /// 子项根容器
    /// </summary>
    Transform mItemRootContainer = null;

    #region OnInitItemRoot 初始化子项根
    /// <summary>
    /// 根窗口名称
    /// </summary>
    readonly string mrItemRootContainerName = "ItemRootContainer";
    /// <summary>
    /// 初始化子项根
    /// </summary>
    void OnInitItemRoot()
    {
        mItemRootContainer = null;
        foreach (Transform key in viewRect)
        {
            if (key.name == mrItemRootContainerName)
            {
                mItemRootContainer = key;
                break;
            }
        }
        if (mItemRootContainer == null)
        {
            mItemRootContainer = new GameObject(mrItemRootContainerName).transform;
        }        
        mItemRootContainer.SetParent(viewRect);
        mItemRootContainer.localPosition = Vector3.zero;
        mItemRootContainer.localEulerAngles = Vector3.zero;
        mItemRootContainer.localScale = Vector3.one;
        mItemRootContainer.gameObject.SetActive(false);

        foreach (Transform key in content)
        {
            key.SetParent(mItemRootContainer);
        }
    }
    #endregion

    #region OnGetItem 获得子项
    /// <summary>
    /// 使用中的item映射
    /// Key:GameObject.InstanceID
    /// Value:子项
    /// </summary>
    Dictionary<int, SFUI_ScrollRectItem> mUsingItemMaping = new Dictionary<int, SFUI_ScrollRectItem>();

    /// <summary>
    /// 缓存item映射
    /// Key: TempleteGameObjectInstanceId
    /// Value:缓存Item
    /// </summary>
    Dictionary<int, Queue<SFUI_ScrollRectItem>> mCacheItemMaping = new Dictionary<int, Queue<SFUI_ScrollRectItem>>();
    /// <summary>
    /// 获得子项
    /// </summary>
    /// <param name="_item">子项</param>
    /// <returns>子项</returns>
    SFUI_ScrollRectItem OnGetItem(Transform _item)
    {
        return mUsingItemMaping[_item.gameObject.GetInstanceID()];
    }
    #endregion

    #region OnCreateItem 创建子项
    /// <summary>
    /// 创建子项
    /// </summary>
    /// <param name="_dataIdx">数据索引</param>
    /// <param name="_orderIdx">编号索引</param>
    /// <returns>子项</returns>
    SFUI_ScrollRectItem OnCreateItem(int _dataIdx,int _orderIdx)
    {
        SFUI_ScrollRectItem result = null;
        GameObject templete = mFuncGetItemTemplete(_dataIdx);
        if (templete != null)
        {
            int tempId = templete.GetInstanceID();
            Queue<SFUI_ScrollRectItem> cache;
            if (mCacheItemMaping.TryGetValue(tempId, out cache) && cache.Count > 0)
            {
                result = cache.Dequeue();
            }
            if (result == null)
            {
                GameObject item = Instantiate(templete);
                result = item.GetOrAddComponent<SFUI_ScrollRectItem>();
                result.SetTempleteArgs(templete.GetInstanceID(), this, mFuncBindSimulateBehaviour?.Invoke(result));
            }
            result.SetDataIndex(_dataIdx, _orderIdx);
            mUsingItemMaping.Add(result.gameObject.GetInstanceID(), result);
            mActionUpdateItem?.Invoke(result);
            result.transform.SetParent(content, false);
        }
        else
        {
            Debug.LogError($"The ItemTemplete with index {_dataIdx} was not found.");
        }
        return result;
    }
    #endregion

    #region OnRecycleItem 回收子项
    /// <summary>
    /// 回收子项
    /// </summary>
    /// <param name="_item">子项</param>
    void OnRecycleItem(Transform _item)
    {
        SFUI_ScrollRectItem mono = OnGetItem(_item);
        if (mono != null)
        {
            mUsingItemMaping.Remove(mono.gameObject.GetInstanceID());
            _item.SetParent(mItemRootContainer, false);
            Queue<SFUI_ScrollRectItem> cache;
            if (!mCacheItemMaping.TryGetValue(mono.templeteGameObjectInstanceId, out cache))
            {
                cache = new Queue<SFUI_ScrollRectItem>();
                mCacheItemMaping.Add(mono.templeteGameObjectInstanceId, cache);
            }
            cache.Enqueue(mono);
        }
    }
    #endregion
}
